/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.command;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.ListIterator;

import javax.swing.JInternalFrame;

import br.ufmg.dees.insane.commons.command.Command;
import br.ufmg.dees.insane.commons.controller.Controller;
import br.ufmg.dees.insane.commons.view.DrawingArea;
import br.ufmg.dees.insane.model.geo.GLine;
import br.ufmg.dees.insane.model.geo.GPoint;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;

/**
 * A command to add lines, or just the initial or final point of a line,
 * using the mouse to enter data (the coords of the points).
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @since January 2004
 */
public class AddLineMouseCommand implements Command, Cloneable {
    
    private InsaneInternalInterface parent;
    private Controller controller;
    private DrawingArea da;
    private int clicks = 0;
    private GPoint ini;
    private GPoint fin;
    private GLine l;
	private ArrayList<GPoint> newPoints = new ArrayList<GPoint>();
    
//*****************************************************************************
	
    /** Constructor method
	* @param parent The parent of this command.
	*/
    public AddLineMouseCommand(JInternalFrame parent) {
        this.parent = (InsaneInternalInterface)parent;
        controller = this.parent.getController();
        da = this.parent.getDrawing();
    }
    
//*****************************************************************************
    
	/** The execute method.<br>
	* It adds lines, or just the initial or final point of a line,
	* using the mouse to enter data (the coords of the points).
	*/
    public void execute() {
        
        Point2D po = da.getWorldPoint();
        
        clicks++;
		
        if (clicks % 2 != 0){
			if (!controller.contains(po)){
				GPoint p = new GPoint(po);
				controller.add(p);
				da.repaint();
				ini = (GPoint)controller.get(po);
				newPoints.add((GPoint)controller.get(po));
				parent.addOutputText(parent.getResourceBundle().getString("cmIniPointCreated") + " " + ini.getX() + ", " + ini.getY());
            }else{
                ini = (GPoint)controller.get(po);
                parent.addOutputText(parent.getResourceBundle().getString("cmIniPointChosen") + " " + ini.getX() + ", " + ini.getY());
            }
            PreviewLineCommand pl = new PreviewLineCommand(parent);
            pl.setInitialPoint(da.getViewPoint(ini));
			da.setMovedCommand(pl);
            da.setRepainting(true);
            da.repaint();
			parent.addOutputText(parent.getResourceBundle().getString("cmSpecFinalPoint"));
			parent.getConsole().setLastPoint(new Point2D.Double(ini.getX(), ini.getY()));
        }else{
			if(po.getX()!=ini.getX() || po.getY()!=ini.getY()) {
				if (!controller.contains(po)){
					GPoint p = new GPoint(po);
					controller.add(p);
					da.repaint();
					fin = (GPoint)controller.get(po);
					newPoints.add((GPoint)controller.get(po));
					parent.addOutputText(parent.getResourceBundle().getString("cmFinalPointCreated") + " " + fin.getX() + ", " + fin.getY());
				}else{
					fin = (GPoint)controller.get(po);
					parent.addOutputText(parent.getResourceBundle().getString("cmFinalPointChosen") + " " + fin.getX() + ", " + fin.getY());
				}
				da.setMovedCommand(null);
				da.setRepainting(false);
				da.setShape(null);
				
				l = new GLine(ini, fin);
				controller.add(l);
				da.repaint();
				parent.addToUndo((Command)this.clone());
				newPoints.clear();
				parent.getConsole().setLastPoint(new Point2D.Double(fin.getX(), fin.getY()));
				parent.addOutputText(parent.getResourceBundle().getString("cmSpecifyIniPoint"));
				
			} else {
				parent.addOutputText(parent.getResourceBundle().getString("cmMustBeDifferent"));
				parent.addOutputText(parent.getResourceBundle().getString("cmSpecFinalPoint"));
				clicks--;
			}
		}
    }
	
//*****************************************************************************
	
	/** The undo method */
    public void undo() {
        controller.remove(l);
		ListIterator newp = newPoints.listIterator();
		while (newp.hasNext()) {
			controller.remove((GPoint)newp.next());
		}
    }
    
//*****************************************************************************
	
	/** The redo method */
    public void redo() {
		ListIterator newp = newPoints.listIterator();
		while (newp.hasNext()) {
			controller.add((GPoint)newp.next());
		}
		controller.add(l);
    }
    
//*****************************************************************************
	
	/** The clone method */
    @SuppressWarnings("unchecked")
	public Object clone() {
        try {
            AddLineMouseCommand cmd = (AddLineMouseCommand)super.clone();
            cmd.l = (GLine)l.clone();
			cmd.newPoints = (ArrayList<GPoint>)newPoints.clone();
			return cmd;
        }catch (CloneNotSupportedException e) {
            throw new Error("Clonning ERROR - This should never happen!");
        }
    }
	
//*****************************************************************************
	
	/** Returns the initial point of the line being created.<br>
	* It's used to allow the use of both methods of inputing data (mouse and keyboard).
	* @return The initial point of the line being created.
	*/
	public GPoint getInitialPoint() {
		return ini;
	}
	
//*****************************************************************************
	
	/** Sets the initial point of the line being created.<br>
	* It's used to avoid errors when mixing both methods of inputing data (mouse and keyboard).
	* @param a The initial point of the line being created.
	*/
	public void setInitialPoint(GPoint a) {
		ini = a;
	}
	
//*****************************************************************************
	
	/** Sets the final point of the line being created.<br>
	* It's used to avoid errors when mixing both methods of inputing data (mouse and keyboard).
	*@param a The final point of the line being created.
	*/
	public void setFinalPoint(GPoint a) {
		fin = a;
	}
	
//*****************************************************************************
	
	/** Returns the final point of the line being created.<br>
	* It's used to avoid errors when mixing both methods of inputing data (mouse and keyboard).
	*@return The final point of the line being created.
	*/
	public GPoint getFinalPoint() {
		return fin;
	}
	
//*****************************************************************************
	
	/** Sets the line being created.<br>
	* It's used to avoid errors when mixing both methods of inputing data (mouse and keyboard).
	*@param a The line being created.
	*/
	public void setLine(GLine a) {
		l = a;
	}
	
//*****************************************************************************
	
	/** Returns the line being created.<br>
	* It's used to avoid errors when mixing both methods of inputing data (mouse and keyboard).
	*@return The line being created.
	*/
	public GLine getLine() {
		return l;
	}
//*****************************************************************************
	
	/** Sets the value of Clicks.
	*@param a The value of Clicks to be set.
	*/
	public void setClicks(int a) {
		clicks = a;
	}
	
//*****************************************************************************
	
	/** Returns the value of Clicks.
	*@return The value of Clicks.
	*/
	public int getClicks() {
		return clicks;
	}
	
//*****************************************************************************
	
	/** Adds a GPoint to the newPoints ArrayList.
	*@param a The GPoint to be added.
	*/
	public void addNewPoint(GPoint a) {
		newPoints.add(a);
	}
	
//*****************************************************************************
	
	/** Clears the newPoints ArrayList.*/
	public void clearNewPoints() {
		newPoints.clear();
	}
	
//*****************************************************************************
}

